/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atjava.jcrweb.Constants;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.DirectoryInfo;
import com.atjava.jcrweb.info.UserInfo;
import com.atjava.jcrweb.manager.DataHelper;
import com.atjava.jcrweb.manager.DirectoryManager;
import com.atjava.jcrweb.manager.PackingToFlexHelper;
import com.atjava.jcrweb.manager.UserManager;
/**
 * 
 * 
 * @author biggie
 * @version $Revision: 1.6 $ $Date: 2008/07/23 08:27:11 $
 */
public class TreeFlexServlet extends FlexBaseServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException , JcrWebException ,Exception{

		DirectoryManager manager = DirectoryManager.getInstance();
		
		String path = getData(inputdata, "path");
		
		UserManager userManager = UserManager.getInstance();
		UserInfo loginUserInfo = userManager.getLoginUserinfo(request.getSession());
		List allList = new ArrayList();
		
		if((path.length() == 0 || path.equals("/")) && !Constants.ADMIN_USERNAME.equals(loginUserInfo.getUserCode())){
			if(loginUserInfo.getPermissionPath()!=null){
				for(int i=0;i<loginUserInfo.getPermissionPath().length;i++){
					String perpath = loginUserInfo.getPermissionPath()[i];
					if("/".equals(perpath)){
						allList = manager.getChildren(perpath);
						break;
					}else{
						allList.add(manager.getDirectory(perpath));
					}
				}				
			}
		}else{
			allList = manager.getChildren(path);
			allList = DataHelper.permissionDirpathFilter(loginUserInfo, allList);
		}
		return toTree(allList);
	}
	private String toTree(List list){
		
		PackingToFlexHelper helper = new PackingToFlexHelper();
		if(list != null && list.size() > 0){
			helper.setResultSize(String.valueOf(list.size()));
		}else{
			return helper.toString();
		}
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<list.size();i++){
			DirectoryInfo dirInfo = (DirectoryInfo)list.get(i);
			sb.append("<"+(i+1)+">");
			sb.append("<node ");
			sb.append(" id=\""+dirInfo.getId()+"\" ");
			sb.append(" label=\""+dirInfo.getName()+"\" ");
			sb.append(" path=\""+dirInfo.getPath()+"\" ");
			sb.append(" >");
			sb.append("</node>");
			sb.append("</"+(i+1)+">");
		}
		helper.setIsError(false);
		helper.setResultData(sb.toString());
		return helper.toString();
	}
}
